#include "StdAfx.h"
#include "PathsDrawElement.h"
#include "SlowMotion.h"
#include "QuickMotion.h"
#include <fstream>
#include <algorithm>
using namespace std;

PathsDrawElement::PathsDrawElement(const vector<ConfigurationState*> * states)
{
	int ss = states->size()*3;
	this->vertices = new GLdouble[ss];
	ConfigurationState * last = nullptr;
	GLdouble * _vertices = this->vertices;
	int _verticesCount = 0;
	
	for_each(states->begin(),states->end(),[& _vertices,& _verticesCount](ConfigurationState * state){
		SlowMotion * sm = dynamic_cast<SlowMotion*> (state);
		QuickMotion * qm = dynamic_cast<QuickMotion*>(state);

		if(sm!=nullptr || qm!=nullptr)
		{
			(_vertices[(_verticesCount)++]) = sm!=nullptr? sm->z:qm->z;
			(_vertices[(_verticesCount)++]) = sm!=nullptr? sm->y:qm->y;
			(_vertices[(_verticesCount)++]) = sm!=nullptr? sm->x:qm->x;
		}
	});
	this->verticesCount = _verticesCount;
	this->indices = new GLint[this->verticesCount/3];

	for(int i=0;i<this->verticesCount/3;++i)
	{
		this->indices[i] = i;
	}
}

PathsDrawElement::~PathsDrawElement(void)
{
	delete [] this->indices;
	delete [] this->vertices;
}

void PathsDrawElement::Draw()
{
	glColor3f(1.0,0.0,0.0);
	glLineWidth(5.0f);
	/*glEnableClientState(GL_VERTEX_ARRAY);
	glColor3f(1.0,0.0,0.0);
	glLineWidth(5.0f);
	glVertexPointer(3,GL_DOUBLE,0,this->vertices);	
	int count = this->verticesCount/3;
	glDrawElements(GL_LINE_STRIP,count,GL_INT,this->indices);
	glFinish();
	glDisableClientState(GL_VERTEX_ARRAY);
	glLineWidth(1.0f);*/
	glBegin(GL_LINE_STRIP);
	int i=0;
	for(int j=0;j<this->verticesCount/3;++j)
	{
		glVertex3d(this->vertices[i++],this->vertices[i++],this->vertices[i++]);
	}
	glEnd();
	glLineWidth(1.0f);
}
